import {
  listComExport,
  getComExport,
  delComExport,
  addComExport,
  updateComExport,
} from "@/apis/user/comExport";
import { getCookiesUserInfo } from "@/utils/auth";

export const api = {
  list: async (queryParams) => {
    let params = Object.assign({}, queryParams);
    params.companyId = JSON.parse(getCookiesUserInfo()).companyId;
    if (queryParams.transactionDate && queryParams.transactionDate.length > 1) {
      params.transactionDateStart = queryParams.transactionDate[0];
      params.transactionDateEnd = queryParams.transactionDate[1];
      params.transactionDate = null;
    }

    let res = await listComExport(params);
    return res;
  },
  one: getComExport,
  delete: delComExport,
  add: async (queryParams) => {
    queryParams.companyId = JSON.parse(getCookiesUserInfo()).companyId;
    let res = await addComExport(queryParams);
    return res;
  },
  update: updateComExport,
  download: (queryParams) => {
    this.download(
      "company/ComExport/export",
      {
        ...queryParams,
      },
      `ComExport_${new Date().getTime()}.xlsx`
    );
  },
};

const MetaData = [
  {
    title: "ID",
    dataIndex: "id",
    dataType: "Number",
    hidden: true,
    inQuery: false,
    inTable: true,
    inForm: true,
    width: 50,
    fixed: true,
  },
  {
    title: "交易日期",
    dataIndex: "transactionDate",
    dataType: "Date",
    inQuery: true,
    inTable: true,
    inForm: true,
    fixed: true,
  },
  {
    title: "类型",
    dataIndex: "type",
    dataType: "Dict",
    inQuery: true,
    inTable: true,
    inForm: true,
    fixed: true,
    options: [
      {
        value: 0,
        label: "进口",
      },
      {
        value: 1,
        label: "出口",
      },
    ],
  },
  {
    title: "合同编号",
    dataIndex: "contractNumber",
    dataType: "Text",
    inQuery: true,
    inTable: true,
    inForm: true,
  },
  {
    title: "发票编号",
    dataIndex: "invoiceNumber",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "商品描述",
    dataIndex: "prodDescription",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "商品编码",
    dataIndex: "hsCode",
    dataType: "Text",
    inQuery: true,
    inTable: true,
    inForm: true,
  },
  {
    title: "数量",
    dataIndex: "quantity",
    dataType: "Number",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "单位",
    dataIndex: "unit",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "单价",
    dataIndex: "unitPrice",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "总价",
    dataIndex: "totalValue",
    dataType: "Text",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "货币",
    dataIndex: "currency",
    dataType: "Text",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "汇率",
    dataIndex: "rate",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "运输方式",
    dataIndex: "mode",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "起运港/机场",
    dataIndex: "departure",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "目的港/机场",
    dataIndex: "destination",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "发货人",
    dataIndex: "consignor",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "收货人",
    dataIndex: "consignee",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "通知人",
    dataIndex: "notify",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "报关行",
    dataIndex: "broker",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "海关申报编号",
    dataIndex: "declarationNumber",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "关税和税费",
    dataIndex: "taxes",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "原产地",
    dataIndex: "origin",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "附件",
    dataIndex: "attachments",
    dataType: "File",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "最终目的地",
    dataIndex: "finalDest",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "状态",
    dataIndex: "status",
    dataType: "Dict",
    inQuery: false,
    inTable: true,
    inForm: true,
    options: [
      {
        value: 0,
        label: "不可用",
      },
      {
        value: 1,
        label: "可用",
      },
    ],
  },
];

export const queryItems = () => {
  let items = MetaData.filter((item) => {
    return item.inQuery;
  }).map((item) => {
    var newItem = {
      title: item.title,
      dataIndex: item.dataIndex,
    };
    // 默认组件
    if (item.dataType == "Number") {
      newItem.component = "Input-number";
    } else if (item.dataType == "Text") {
      newItem.component = "Input";
    } else if (item.dataType == "Dict") {
      newItem.component = "Select";
      newItem.componentProps = {
        options: item.options,
      };
    } else if (item.dataType == "Date") {
      newItem.component = "Range-picker";
    }
    return newItem;
  });
  return items;
};

export const columns = () => {
  let items = MetaData.filter((item) => {
    return item.inTable;
  }).map((item) => {
    var newItem = {
      title: item.title,
      dataIndex: item.dataIndex,
      key: item.dataIndex,
      align: "center",
      width: item.width || 200,
    };
    if (item.fixed) {
      newItem.fixed = item.fixed;
    }
    if (item.dataType == "Dict") {
      newItem.customRender = (text, record, index) => {
        let option = item.options.find((option) => {
          return option.value == record[item.dataIndex];
        });
        return option ? option.label : "-";
      };
    }
    return newItem;
  });
  items.push({
    title: "操作",
    dataIndex: "action",
    key: "action",
    align: "center",
    width: 180,
    fixed: "right",
    scopedSlots: { customRender: "action" },
  });
  return items;
};

export const formItems = () => {
  let items = MetaData.filter((item) => {
    return item.inForm && !item.hidden;
  }).map((item) => {
    var newItem = {
      title: item.title,
      dataIndex: item.dataIndex,
    };
    // 默认组件
    if (item.dataType == "Number") {
      newItem.component = "Input-number";
    } else if (item.dataType == "Text") {
      newItem.component = "Input";
    } else if (item.dataType == "Dict") {
      newItem.component = "Select";
      newItem.componentProps = {
        options: item.options,
      };
    } else if (item.dataType == "Date") {
      newItem.component = "Date-picker";
    } else if (item.dataType == "File") {
      newItem.component = "Upload";
    }
    return newItem;
  });
  return items;
};

export const rules = {
  transactionDate: [{ required: true, message: "必须输入交易日期" }],
  type: [{ required: true, message: "必须选择类型" }],
  contractNumber: [{ required: true, message: "必须输入合同编号" }],
  totalValue: [{ required: true, message: "必须输入总价" }],
  currency: [{ required: true, message: "必须输入货币" }],
  rate: [{ required: true, message: "必须输入汇率" }],
  declarationNumber: [{ required: true, message: "必须输入海关申报编号" }],
};

export const formConfg = function () {
  var config = {
    group: 2,
    labelCol: { span: 6 },
    wrapperCol: { span: 14 },
  };
  var items = formItems();

  // 计算分列
  let half = items.length / 2;
  let yu = items.length % 2;
  if (yu > 0) {
    half = half + 1;
  }
  config.formItems = [items.slice(0, half), items.slice(half, items.length)];
  return config;
};
